package com.weareint.biz.controller;

import com.weareint.biz.vo.ProductBomVo;
import com.weareint.biz.search.ProductBomSearch;
import com.weareint.biz.service.IProductBomService;
import com.weareint.common.jpabase.ResultVoUtil;
import com.weareint.common.jpabase.ResultVo;
import com.weareint.common.jpabase.PageVo;
import com.weareint.common.jpabase.PageUtil;
import org.springframework.data.domain.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiImplicitParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <pre>
 *      产品BOM 前端控制器
 * </pre>
 *
 * @author Johnny
 * @date 2021-09-28
 */
@Api(tags = "产品BOM")
@RestController
@Slf4j
@RequestMapping("/api/productBom")
public class ProductBomController {

    private final IProductBomService productBomService;

    public ProductBomController(IProductBomService productBomService) {
        this.productBomService = productBomService;
    }

    /**
     * 带条件查询的 分页接口
     *
     * @param productBomSearch : 查询条件
     * @param pageVo           : 分页条件
     * @return : ResultVo<Page<ProductBomVo>>
     */
    @GetMapping("/listByCondition")
    @ApiOperation(value = "根据条件查询产品BOM (分页)")
    public ResultVo<Page<ProductBomVo>> listByCondition(ProductBomSearch productBomSearch,
                                                        PageVo pageVo) {
        Page<ProductBomVo> page = productBomService.listByCondition(productBomSearch, PageUtil.initPage(pageVo));
        return ResultVoUtil.success(page);
    }

    @PostMapping("/add")
    @ApiOperation(value = "新增产品BOM信息")
    public ResultVo<Void> add(@RequestBody @Valid ProductBomVo productBomVo) {
        productBomService.add(productBomVo);
        return ResultVoUtil.success();
    }

    @PostMapping("/edit")
    @ApiOperation(value = "修改产品BOM信息")
    public ResultVo<Void> edit(@RequestBody @Valid ProductBomVo productBomVo) {
        productBomService.edit(productBomVo);
        return ResultVoUtil.success();
    }

    @GetMapping("/delete/{id}")
    @ApiOperation(value = "删除产品BOM信息")
    @ApiImplicitParam(name = "id", value = "产品BOMid", dataType = "Long", example = "1", dataTypeClass = Long.class)
    public ResultVo<Void> delete(@PathVariable Long id) {
        productBomService.delete(id);
        return ResultVoUtil.success();
    }

    @PostMapping("/deletes")
    @ApiOperation(value = "批量删除产品BOM信息")
    @ApiImplicitParam(name = "ids", value = "产品BOMids", dataType = "Long[]", dataTypeClass = Arrays.class)
    public ResultVo<Void> deletes(Long[] ids) {
        List<Long> idList = Arrays.stream(ids).collect(Collectors.toList());
        productBomService.deletes(idList);
        return ResultVoUtil.success();
    }

}
